import EventEmitter from "events";
export class TextModel extends EventEmitter {
  spacers_ = "";
  constructor() {
    super();
  }
  textChangeEmit_() {
    this.emit("text-change");
  }
  getSpacers() {
    return this.spacers_;
  }
  getSpacersByIndex(startIndex, endIndex) {
    return this.spacers_.slice(startIndex, endIndex);
  }
  insert(spacerIndex, spacers) {
    const oldSpacers = this.spacers_;
    this.spacers_ =
      oldSpacers.slice(0, spacerIndex) +
      spacers +
      oldSpacers.slice(spacerIndex);
    this.textChangeEmit_();
  }
  remove(startIndex, endIndex) {
    const oldSpacers = this.spacers_;
    if (startIndex > endIndex) {
      [startIndex, endIndex] = [endIndex, startIndex];
    }
    this.spacers_ =
      oldSpacers.slice(0, startIndex) + oldSpacers.slice(endIndex);
    this.textChangeEmit_();
  }
}

export default TextModel;
